package handler

import (
	"context"
	"errors"
	"fmt"
	"github.com/google/uuid"
	"go.uber.org/zap"
	"strconv"
	"takeout_server/basic/dao"
	"takeout_server/basic/pkg"
	__ "takeout_server/basic/proto/order"
)

type OrderServer struct {
	__.UnimplementedOrderServerServer
}

func (s *OrderServer) OrderAdd(_ context.Context, req *__.OrderAddReq) (*__.OrderAddResp, error) {
	var OrderPrice float64
	//判断是否是购物车添加或者商品详情添加订单
	if req.ShoppingCartId != 0 {
		//购物车查找
		shoppingWhere, err := dao.ShoppingWhere(req.ShoppingCartId)
		if err != nil {
			zap.L().Info("购物车查询失败")
			return nil, err
		}
		zap.L().Info(fmt.Sprintf("购物车查询成功%v", shoppingWhere))
		OrderPrice = shoppingWhere.Price * float64(shoppingWhere.Sum)
	} else if req.GoodId != 0 {
		//商品查找
		goodWhere, err := dao.GoodWhere(req.GoodId)
		if err != nil {
			zap.L().Info("商品查询失败")
			return nil, err
		}
		zap.L().Info(fmt.Sprintf("商品查询成功%v", goodWhere))
		OrderPrice = goodWhere.Price * float64(req.Sum)
	}
	//订单添加

	OrderNumber := uuid.New().String()

	order, err := dao.OrderAdd(req.UserId, OrderNumber, OrderPrice, uint(req.ShoppingCartId), uint(req.GoodId))
	if err != nil {
		zap.L().Info("订单添加失败")
		return nil, err
	}
	if order.Id == 0 {
		zap.L().Info("订单添加失败失败")
		return nil, err
	}
	//移出购物车
	shoppingDelete, err := dao.ShoppingDelete(req.ShoppingCartId)
	if err != nil {
		zap.L().Info("购物车删除失败")
		return nil, err
	}
	zap.L().Info(fmt.Sprintf("%v", shoppingDelete))
	//支付
	OrderPrice2 := strconv.FormatFloat(order.OrderPrice, byte(2), -1, 32)
	alp := pkg.AliPay(order.OrderNumber, OrderPrice2)
	fmt.Println(alp)

	//修改订单状态

	if !dao.OrderUpdate(order.Id, 1) {
		zap.L().Info("订单修改数量失败")
		return nil, errors.New("修改失败")
	}

	return &__.OrderAddResp{OrderId: int64(order.Id)}, nil
}
func (s *OrderServer) OrderWhere(_ context.Context, req *__.OrderWhereReq) (*__.OrderWhereResp, error) {
	where, err := dao.OrderWhere(req.Size, req.Page)
	fmt.Println(where)
	if err != nil {
		zap.L().Info("订单查询失败")
		return nil, err
	}
	orderwhere := []*__.OrderList{}
	for _, order := range where {
		goodWhere, err := dao.GoodWhere(int32(order.GoodId))
		if err != nil {
			return nil, err
		}

		orderwhere = append(orderwhere, &__.OrderList{
			OrderNumber: order.OrderNumber,
			OrderPrice:  float32(order.OrderPrice),
			Img:         goodWhere.Img,
			Name:        goodWhere.Name,
			Price:       float32(order.OrderPrice),
		})
	}
	return &__.OrderWhereResp{List: orderwhere}, nil
}
func (s *OrderServer) OrderWhereOne(_ context.Context, req *__.OrderWhereOneReq) (*__.OrderWhereOneResp, error) {
	var where, err = dao.GoodWhereOne(req.Name)
	if err != nil {
		zap.L().Info("订单查询失败")
		return nil, err
	}
	orderWheres, err := dao.OrderWheres(where.Id)
	if err != nil {
		return nil, err
	}
	orderwhere := []*__.OrderList{}
	for _, order := range orderWheres {
		goodWhere, err := dao.GoodWhere(int32(order.GoodId))
		if err != nil {
			return nil, err
		}

		orderwhere = append(orderwhere, &__.OrderList{
			OrderNumber: order.OrderNumber,
			OrderPrice:  float32(order.OrderPrice),
			Img:         goodWhere.Img,
			Name:        goodWhere.Name,
			Price:       float32(order.OrderPrice),
		})
	}
	return &__.OrderWhereOneResp{List: orderwhere}, nil
}
func (s *OrderServer) OrderUpdateOne(_ context.Context, req *__.OrderUpdateOneReq) (*__.OrderUpdateOneResp, error) {
	if !dao.GoodUpdateOne(req.Id) {

		return nil, errors.New("取消订单失败")
	}

	return &__.OrderUpdateOneResp{Update: true}, nil
}
func (s *OrderServer) OrderTimeOne(_ context.Context, req *__.OrderTimeOneReq) (*__.OrderTimeOneResp, error) {

	return &__.OrderTimeOneResp{Time: "请尽快支付"}, nil
}
